package com.movitech.mobile.ddzy.bpm.service.impl;

import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.modules.config.*;
import com.movitech.mobile.cz.base.modules.config.util.ActionTypeEnumUtil;
import com.movitech.mobile.cz.base.supplier.entity.SHistoryComment;
import com.movitech.mobile.cz.base.supplier.entity.SMyDeals;
import com.movitech.mobile.cz.base.supplier.entity.SSupplierRcRelate;
import com.movitech.mobile.cz.base.supplier.entity.SSuppliers;
import com.movitech.mobile.cz.base.supplier.entity.ext.ExtSSuppliers;
import com.movitech.mobile.cz.base.supplier.mapper.SSupplierRcRelateMapper;
import com.movitech.mobile.cz.base.supplier.mapper.ext.ExtSSuppliersMapper;
import com.movitech.mobile.cz.base.supplier.service.SCommentConfigService;
import com.movitech.mobile.cz.base.supplier.service.SHistoryCommentService;
import com.movitech.mobile.cz.base.supplier.service.SMyDealsService;
import com.movitech.mobile.cz.base.supplier.service.SSuppliersService;
import com.movitech.mobile.ddzy.bpm.entity.BpmRelatedType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.ddzy.bpm.entity.SBpm;
import com.movitech.mobile.ddzy.bpm.mapper.SBpmMapper;
import com.movitech.mobile.ddzy.bpm.mapper.ext.ExtSBpmMapper;
import com.movitech.mobile.ddzy.bpm.service.SBpmService;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * bpm对接表 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-15
 */
@Service("sBpmService")
public class SBpmServiceImpl extends ServiceImpl<SBpmMapper, SBpm> implements SBpmService {

    @Autowired
    private ExtSBpmMapper extSBpmMapper;
    @Autowired
    private SCommentConfigService sCommentConfigService;
    @Autowired
    private SMyDealsService sMyDealsService;
    @Autowired
    private SSuppliersService sSuppliersService;
    @Autowired
    private SSupplierRcRelateMapper sSupplierRcRelateMapper;
    @Autowired
    private SHistoryCommentService sHistoryCommentService;

    @Override
    public void updateSBpmSelective(SBpm entity) {
        extSBpmMapper.updateSBpmSelective(entity);
    }

    @Override
    public boolean bpmRejected(SBpm entity) {
        if (entity != null && BpmRelatedType.SupplierStorageFlow.getCode().equals(entity.getBpmRelatedType())) {
            //  插入被驳回审批历史
            //Step2: 审批历史记录更新
            SHistoryComment sHistoryComment = new SHistoryComment();

            sHistoryComment.setModelType(HistoryCommentModelTypeEnum.AUDIT_PROCESS_SUPPLIERS_INFO.getCode()); //供方信息审核历程
            sHistoryComment.setRelatedId(entity.getRelatedId());
            sHistoryComment.setSubRelatedId(entity.getSubRelatedId());
            sHistoryComment.setCommentType(HistoryCommentTypeEnum.INTRANET.getCode());  //内外网共用
            sHistoryComment.setNodeName(sCommentConfigService.getValueByCode(CommentConfigEnum.BPM_STORE_APPROVE_NODE_NAME.getCode()));
            sHistoryComment.setCommentContent("供方入库审批被打回");

            sHistoryComment.setActionName(ActionTypeEnumUtil.actionTypeEnumMap.get(ActionTypeEnum.MBS_AUDIT_STATUS_BACK));
            sHistoryComment.setAgreeFlag(CommonConstants.NO_AGREE_FLAG);

            sHistoryCommentService.saveSHistoryComment(sHistoryComment);
            //  更新供方状态为被驳回

            SSupplierRcRelate extSSuppliers = new SSupplierRcRelate();
            extSSuppliers.setId(entity.getSubRelatedId());
            extSSuppliers.setSupplierStatus(99); //表示入库审批被打回
            sSupplierRcRelateMapper.updateById(extSSuppliers);

            //  入库审批被驳回生成被驳回提醒；
//            SSuppliers sSuppliers = sSuppliersService.getById(entity.getRelatedId());
//            String titleTemplate = sCommentConfigService.getValueByCode(CommentConfigEnum.MY_DEAL_TEMPLATE.getCode());
//            String title = MessageFormat.format(titleTemplate, MyDealSubTypeEnum.STORE_APPOVE.getDesc(), sSuppliers.getCompanyName() +"入库审批被驳回,"/* + MyDealSubTypeEnum.SITE_INSPECTION.getDesc()*/);
//            SMyDeals newEntity = new SMyDeals();
//            newEntity.setUserId(entity.getCreateBy());
//            newEntity.setDealType(DealTypeEnum.SUPPLIERS.getCode());
//            newEntity.setDealSubType(MyDealSubTypeEnum.STORE_APPOVE.getCode());
//            newEntity.setRelatedId(entity.getRelatedId());
//            newEntity.setSubRelatedId(entity.getSubRelatedId());
//            newEntity.setThirdRelatedId(entity.getWorkFlowId());
//            newEntity.setDealTitle(title);
//            newEntity.setActionType(MyDealActionTypeEnum.STORE_APPOVE_REJECTION.getCode());
//            sMyDealsService.saveSMyDeals(newEntity);
        }
        return true;
    }

    @Override
    public String getBpmWorkflowId(Map<String, Object> params) {
        return extSBpmMapper.getBpmWorkflowId(params);
    }
}
